package org.jix.framework.biz.context.holder;

import com.alibaba.ttl.TransmittableThreadLocal;
import org.jix.framework.common.constant.GlobalConstants;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author stark
 * @date 2025/6/3 12:28
 * @description
 **/
public class LoginUserContextHolder {

    //初始化一个threadLocal变量
    private static final ThreadLocal<Map<String,Object>> LOGIN_USER_CONTEXT_THREAD_LOCAL
            = TransmittableThreadLocal.withInitial(HashMap::new);

    /**
     * 设置UserID
     * @param value
     */
    public static void setUserID(Object value){
        LOGIN_USER_CONTEXT_THREAD_LOCAL.get().put(GlobalConstants.USER_ID, value);
    }

    /**
     * 获取UserID
     * @return
     */
    public static Long getUserID(){
        Object value = LOGIN_USER_CONTEXT_THREAD_LOCAL.get().get(GlobalConstants.USER_ID);
        //判空
        if (Objects.isNull(value)){
            return null;
        }
        return Long.valueOf(value.toString());
    }

    /**
     * 删除threadLocal
     */
    public static void remove(){
        LOGIN_USER_CONTEXT_THREAD_LOCAL.remove();
    }
}
